/*  BEGIN_FILE_HDR
******************************************Copyright(C)*****************************************
*
*                                       YKXH  Technology
*
***********************************File Info***************************************************
*   File Name       @: Common.c
************************************************************************************************
*   Project/Product @: Common
*   Title           @: Common Source File
*   Author          @: zhongliang.Yang 
************************************************************************************************
*   Description     @:  This is common module for different driver
*                     
************************************************************************************************
*   Limitations     @: None
*
************************************************************************************************
*
************************************************************************************************
*   Revision History:
* 
*   Version     Date          Initials           CR#         Descriptions
*   --------    -----------   ----------------   --------    -----------------------
*   1.0         2018/05/29    zhongliang.yang    N/A         Original 
*   1.1         xxxx/xx/xx    xxxxx              N/A         xxxxxxxx 
*     
************************************************************************************************
* END_FILE_HDR*/

#include "Common.h"


#ifndef COMMON_C
#define COMMON_C
#endif 

UINT8 SET_BIT_8U(UINT8 x)
{
    return ( UINT8 ) (  ((UINT8)1u<<x) );
}

UINT8 CLR_BIT_8U(UINT8 x)
{
    return ( UINT8 ) (  (~((UINT8)1u<<x)) );
}


UINT16 SET_BIT_16U(UINT8 x)
{
    return ( UINT16 ) (  ((UINT16)1u<<x) );
}
UINT16 CLR_BIT_16U(UINT8 x)
{
    return ( UINT16 ) (  (~(( UINT16 )1u<<x)) );
}


/*BEGIN_FUNCTION_HDR
************************************************************************************************
* Function Name : Dly
* Description   : This function is used to software delay
* 
* 
* Inputs        : ParaDly Delay Time
* 
* 
* Outputs       : None
* 
* 
* Limitations   : OSC 8M Bus 2M, ParaDly = 40, it will delay about 500us
*                                ParaDly = 30, it will delay about 375us
************************************************************************************************
END_FUNCTION_HDR*/
void Dly( UINT16 ParaDly )
{
    UINT16    i;
    for( i=0u;i<ParaDly;i++ )
    {
       YK_FEED_COP(); 
    }
}


/*BEGIN_FUNCTION_HDR
************************************************************************************************
* Function Name : OverTimeCheckByte
* Description   : This function is used for checking overtime when waiting for a value meet.
* 
* 
* Inputs        : UINT8  *pChkByte   :Pointer to variable or register that you want to check
*                 UINT8  ChkBitsMask :Bits you want to check
*                 UINT8  ChkVal      :Value you want "(*pChkByte) & chkBits)" equal to
*                 UINT8  ChkTime     :Counter for overtime
* 
* Outputs       : COMMON_NOT_OVER_TIME :"((*pChkByte) & bitsMask) == chkVal" occurs in checking period 
*                 COMMON_OVER_TIME     :"((*pChkByte) & bitsMask) == chkVal" don't occur in checking period
* 
* 
* Limitations   : 
************************************************************************************************
END_FUNCTION_HDR*/
UINT8 OverTimeCheckByte(volatile UINT8 *pChkByte, UINT8 ChkBitsMask, UINT8 ChkVal, UINT16 ChkTime)
{
    UINT8 ReturnVal;
    
    /* Loop until the value meet or overtime occur */
    while((((*pChkByte) & ChkBitsMask) != ChkVal) && (ChkTime > 0))
    {
        ChkTime--;
        YK_FEED_COP(); /* Feed Watchdog */
    }
    
    if(ChkTime == 0)
    {
        ReturnVal = COMMON_OVER_TIME;      /* Overtime occur     */
    }
    else
    {
        ReturnVal = COMMON_NOT_OVER_TIME;  /* Overtime not occur */
    }

    return(ReturnVal);
}


/*BEGIN_FUNCTION_HDR
************************************************************************************************
* Function Name : OverTimeCheckWord
* Description   : This function is used for checking overtime when waiting for a value meet.
* 
* 
* Inputs        : UINT16  *pChkByte   :Pointer to variable or register that you want to check
*                 UINT16  ChkBitsMask :Bits you want to check
*                 UINT16  ChkVal      :Value you want "(*pChkByte) & chkBits)" equal to
*                 UINT16  ChkTime     :Counter for overtime
* 
* Outputs       : COMMON_NOT_OVER_TIME :"((*pChkByte) & bitsMask) == chkVal" occurs in checking period 
*                 COMMON_OVER_TIME     :"((*pChkByte) & bitsMask) == chkVal" don't occur in checking period
* 
* 
* Limitations   : 
************************************************************************************************
END_FUNCTION_HDR*/
UINT8 OverTimeCheckWord(volatile UINT16 *pChkByte, UINT16 ChkBitsMask, UINT16 ChkVal, UINT16 ChkTime)
{
    UINT8  ReturnVal;
    
    /* Loop until the value meet or overtime occur */
    while((((*pChkByte) & ChkBitsMask) != ChkVal) && (ChkTime > 0))
    {
        ChkTime--;
        YK_FEED_COP(); /* Feed Watchdog */
    }
    
    if(ChkTime == 0)
    {
        ReturnVal = COMMON_OVER_TIME;      /* Overtime occur     */
    }
    else
    {
        ReturnVal = COMMON_NOT_OVER_TIME;  /* Overtime not occur */
    }

 
    
    return(ReturnVal);
}

/*BEGIN_FUNCTION_HDR
************************************************************************************************
* Function Name : OverTimeCheck16
* Description   : This function is used for checking overtime when waiting for a value meet.
*
*
* Inputs        : HRINT16U  *pChkByte   :Pointer to variable or register that you want to check
*                 HRINT16U  ChkBitsMask :Bits you want to check
*                 HRINT16U  ChkVal      :Value you want "(*pChkByte) & chkBits)" equal to
*                 HRINT16U  ChkTime     :Counter for overtime
*
* Outputs       : COMMON_NOT_OVER_TIME :"((*pChkByte) & bitsMask) == chkVal" occurs in checking period
*                 COMMON_OVER_TIME     :"((*pChkByte) & bitsMask) == chkVal" don't occur in checking period
*
*
* Limitations   :
************************************************************************************************
END_FUNCTION_HDR*/
UINT8 OverTimeCheckLong(volatile UINT32 *pChkByte, UINT32 ChkBitsMask, UINT32 ChkVal, UINT16 ChkTime)
{
	UINT8 ReturnVal;

    /* Loop until the value meet or overtime occur */
    while((((*pChkByte) & ChkBitsMask) != ChkVal) && (ChkTime > 0))
    {
        ChkTime--;
        WDog_Feed(); /* Feed Watchdog */
    }

    if(ChkTime == 0)
    {
        ReturnVal = COMMON_OVER_TIME;      /* Overtime occur     */
    }
    else
    {
        ReturnVal = COMMON_NOT_OVER_TIME;  /* Overtime not occur */
    }

    return(ReturnVal);
}
















